package com.ruyiadmin.springboot.web_api.controllers.system_controllers;

import com.alibaba.fastjson.JSON;
import com.ruyiadmin.springboot.web_api.annotations.framework.AllowAnonymous;
import com.ruyiadmin.springboot.web_api.annotations.framework.SimpleRateLimiter;
import com.ruyiadmin.springboot.common_infrastructure.properties.SystemCacheConfig;
import com.ruyiadmin.springboot.common_infrastructure.components.system.RuYiRedisContext;
import com.ruyiadmin.springboot.common_infrastructure.models.SearchCondition;
import com.ruyiadmin.springboot.common_infrastructure.models.QueryResponseResult;
import com.ruyiadmin.springboot.entity_data_model.entity_model.system_model.SysLanguage;
import com.ruyiadmin.springboot.service_layer.interfaces.system_interfaces.ISysLanguageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 系统语言 前端控制器
 * </p>
 *
 * @author RuYiAdmin
 * @since 2022-07-12
 */
@RestController
@RequestMapping("/LanguageManagement")
@Api(tags = "系统多语管理服务")
@RequiredArgsConstructor
public class SysLanguageController {

    //region 服务私有属性

    private final RuYiRedisContext redisContext;
    private final SystemCacheConfig systemCacheConfig;
    private final ISysLanguageService languageService;

    //endregion

    //region 分页查询多语信息

    @PostMapping("/Post")
    @ApiOperation(value = "分页查询多语信息")
    @AllowAnonymous
    @SimpleRateLimiter(value = 1.0)
    //@Log(operationType = OperationType.QueryList)
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public QueryResponseResult<SysLanguage> queryByPage(@RequestBody SearchCondition searchCondition)
            throws ExecutionException, InterruptedException {
        //region 从数据库查询多语

        //QueryWrapper<SysLanguage> wrapper = new QueryWrapper<>();//设置条件
        //queryCondition.convertToQueryWrapper(wrapper);//转化查询条件、转化排序条件
        //Page<SysLanguage> page = new Page<>(queryCondition.getPageIndex(), queryCondition.getPageSize());//初始化page

        //this.languageService.page(page, wrapper);//执行查询
        //long total = page.getTotal();//总数
        //List<SysLanguage> rs = page.getRecords();//结果

        //return QueryResult.success(total, rs);

        //endregion

        CompletableFuture<QueryResponseResult<SysLanguage>> future = CompletableFuture.supplyAsync(() -> {
            Object value = this.redisContext.get(systemCacheConfig.getLanguageCacheName());
            List<SysLanguage> languages = JSON.parseArray(value.toString(), SysLanguage.class);
            return QueryResponseResult.success(languages.size(), languages);
        });
        return future.get();
    }

    //endregion

}
